<!-- build time:Tue Jul 21 2020 12:41:24 GMT+0800 (GMT+08:00) --><!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=2"><meta name="theme-color" content="#222"><meta name="generator" content="Hexo 4.2.0"><link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png"><link rel="icon" type="image/png" sizes="32x32" href="/img/blog_favicon/favicon-32x32-next.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/blog_favicon/favicon-16x16-next.png"><link rel="mask-icon" href="/images/logo.svg" color="#222"><meta name="baidu-site-verification" content="r15J5zOS2KI43yBS"><link rel="stylesheet" href="/css/main.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"><link rel="stylesheet" href="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.css"><link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/themes/blue/pace-theme-minimal.min.css"><script src="//cdnjs.cloudflare.com/ajax/libs/pace/1.0.2/pace.min.js"></script><script id="hexo-configurations">var NexT=window.NexT||{},CONFIG={hostname:new URL("http://www.zhengjiaao.cn").hostname,root:"/",scheme:"Muse",version:"7.6.0",exturl:!1,sidebar:{position:"left",display:"post",padding:18,offset:12,onmobile:!1},copycode:{enable:!0,show_result:!0,style:null},back2top:{enable:!0,sidebar:!1,scrollpercent:!0},bookmark:{enable:!0,color:"#222",save:"auto"},fancybox:!0,mediumzoom:!1,lazyload:!1,pangu:!1,comments:{style:"tabs",active:"valine",storage:!0,lazyload:!1,nav:null,activeClass:"valine"},algolia:{appID:"",apiKey:"",indexName:"",hits:{per_page:10},labels:{input_placeholder:"Search for Posts",hits_empty:"We didn't find any results for the search: ${query}",hits_stats:"${hits} results found in ${time} ms"}},localsearch:{enable:!0,trigger:"auto",top_n_per_article:1,unescape:!1,preload:!1},path:"search.xml",motion:{enable:!0,async:!1,transition:{post_block:"fadeIn",post_header:"slideDownIn",post_body:"slideDownIn",coll_header:"slideLeftIn",sidebar:"slideUpIn"}}}</script><meta property="og:type" content="article"><meta property="og:title" content="spring5x-sharding-jdbc-jpa"><meta property="og:url" content="http://www.zhengjiaao.cn/posts/2467482698.html"><meta property="og:site_name" content="宇宙小神特别萌"><meta property="og:locale" content="zh_CN"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-c35798bfd074d7c5.png"><meta property="og:image" content="https://upload-images.jianshu.io/upload_images/15645795-deb89a831914841d.png"><meta property="article:published_time" content="2020-01-03T08:14:50.000Z"><meta property="article:modified_time" content="2020-07-20T10:37:55.317Z"><meta property="article:author" content="宇宙小神特别萌"><meta property="article:tag" content="spring5-xml方式配置"><meta name="twitter:card" content="summary"><meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/15645795-c35798bfd074d7c5.png"><link rel="canonical" href="http://www.zhengjiaao.cn/posts/2467482698.html"><script id="page-configurations">CONFIG.page={sidebar:"",isHome:!1,isPost:!0}</script><title>spring5x-sharding-jdbc-jpa | 宇宙小神特别萌</title><noscript><style>.sidebar-inner,.use-motion .brand,.use-motion .collection-header,.use-motion .comments,.use-motion .menu-item,.use-motion .pagination,.use-motion .post-block,.use-motion .post-body,.use-motion .post-header{opacity:initial}.use-motion .site-subtitle,.use-motion .site-title{opacity:initial;top:initial}.use-motion .logo-line-before i{left:initial}.use-motion .logo-line-after i{right:initial}</style></noscript><link rel="alternate" href="/atom.xml" title="宇宙小神特别萌" type="application/atom+xml"></head><body itemscope itemtype="http://schema.org/WebPage"><div class="container use-motion"><div class="headband"></div><header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="header-inner"><div class="site-brand-container"><div class="site-meta custom-logo"><div class="site-meta-headline"><a><img class="custom-logo-image" src="/img/blog_favicon/favicon-32x32-next.png" alt="宇宙小神特别萌"></a></div><div><a href="/" class="brand" rel="start"><span class="logo-line-before"><i></i></span> <span class="site-title">宇宙小神特别萌</span> <span class="logo-line-after"><i></i></span></a></div><p class="site-subtitle">ZhengJa</p></div><div class="site-nav-toggle"><div class="toggle" aria-label="切换导航栏"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div></div></div><nav class="site-nav"><ul id="menu" class="menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">27</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">116</span></a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-fw fa-sitemap"></i>站点地图</a></li><li class="menu-item menu-item-messageboard"><a href="/messageboard/" rel="section"><i class="fa fa-fw fa-address-book"></i>留言板</a></li><li class="menu-item menu-item-links"><a href="/links/" rel="section"><i class="fa fa-fw fa-user-plus"></i>友链</a></li><li class="menu-item menu-item-shuoshuo"><a href="/shuoshuo/" rel="section"><i class="fa fa-fw fa-file-text-o"></i>说说</a></li><li class="menu-item menu-item-search"><a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索</a></li></ul></nav><div class="site-search"><div class="popup search-popup"><div class="search-header"><span class="search-icon"><i class="fa fa-search"></i></span><div class="search-input-container"><input autocomplete="off" autocorrect="off" autocapitalize="none" placeholder="搜索..." spellcheck="false" type="text" id="search-input"></div><span class="popup-btn-close"><i class="fa fa-times-circle"></i></span></div><div id="search-result"></div></div><div class="search-pop-overlay"></div></div></div></header><div class="back-to-top"><i class="fa fa-arrow-up"></i> <span>0%</span></div><div class="reading-progress-bar"></div><a role="button" class="book-mark-link book-mark-link-fixed"></a> <a href="https://github.com/zhengjiaao" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin:130px 106px" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><main class="main"><div class="main-inner"><div class="content-wrap"><div class="content"><div class="posts-expand"><article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN"><link itemprop="mainEntityOfPage" href="http://www.zhengjiaao.cn/posts/2467482698.html"><span hidden itemprop="author" itemscope itemtype="http://schema.org/Person"><meta itemprop="image" content="/my-images/avatar.gif"><meta itemprop="name" content="宇宙小神特别萌"><meta itemprop="description" content="人生就是一次幸运的旅行！"></span><span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><meta itemprop="name" content="宇宙小神特别萌"></span><header class="post-header"><h1 class="post-title" itemprop="name headline">spring5x-sharding-jdbc-jpa</h1><div class="post-meta"><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-o"></i> </span><span class="post-meta-item-text">发表于</span> <time title="创建时间：2020-01-03 16:14:50" itemprop="dateCreated datePublished" datetime="2020-01-03T16:14:50+08:00">2020-01-03</time> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-calendar-check-o"></i> </span><span class="post-meta-item-text">更新于</span> <time title="修改时间：2020-07-20 18:37:55" itemprop="dateModified" datetime="2020-07-20T18:37:55+08:00">2020-07-20</time> </span><span id="/posts/2467482698.html" class="post-meta-item leancloud_visitors" data-flag-title="spring5x-sharding-jdbc-jpa" title="热度"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span class="leancloud-visitors-count"></span> <span>℃</span> </span><span class="post-meta-item" title="热度" id="busuanzi_container_page_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="post-meta-item-text">热度：</span> <span id="busuanzi_value_page_pv"></span> </span><span class="post-meta-item"><span class="post-meta-item-icon"><i class="fa fa-comment-o"></i> </span><span class="post-meta-item-text">Valine：</span> <a title="valine" href="/posts/2467482698.html#comments" itemprop="discussionUrl"><span class="post-comments-count valine-comment-count" data-xid="/posts/2467482698.html" itemprop="commentCount"></span></a></span><br><span class="post-meta-item" title="本文字数"><span class="post-meta-item-icon"><i class="fa fa-file-word-o"></i> </span><span class="post-meta-item-text">本文字数：</span> <span>27k</span> </span><span class="post-meta-item" title="阅读时长"><span class="post-meta-item-icon"><i class="fa fa-clock-o"></i> </span><span class="post-meta-item-text">阅读时长 &asymp;</span> <span>24 分钟</span></span></div></header><div class="post-body" itemprop="articleBody"><meta name="referrer" content="no-referrer"><a id="more"></a><p><img src="https://upload-images.jianshu.io/upload_images/15645795-c35798bfd074d7c5.png" alt="spring5x-sharding-jdbc-jpa目录.png"></p><p>spring5x-sharding-jdbc-jpa此模块是从spring5x-data-jpa模块扩展过来的<br>spring5x-data-jpa 模块是一个非常干净的spring5.x+springMVC+jpa架构<br>如果没有搭建spring5x-data-jpa模块 ，请参考 <a href="https://www.jianshu.com/p/2737d5e3a960" target="_blank" rel="noopener">spring5x-data-jpa搭建</a></p><h2 id="搭建项目"><a href="#搭建项目" class="headerlink" title="搭建项目"></a>搭建项目</h2><p><strong>基于spring5x-data-jpa 基础模块 新增功能：</strong></p><ul><li>1、spring5.x集成sharding jdbc依赖</li><li>2、sharding-jdbc xml配置</li><li>3、单元测试(sharding jdbc 分库分表测试)</li><li>4、项目的github和博客地址</li></ul><p><strong>项目架构：spring5.x+jpa+sharding jdbc+druid+mysql</strong><br>说明：<strong>sharding jdbc 作用分库分表</strong> ，具体百度或参考官网： <a href="https://shardingsphere.apache.org/document/current/cn/overview/" target="_blank" rel="noopener">sharding jdbc</a><br>这里只讲spring项目以xml方式如何配置和使用sharding jdbc。<br><strong>分库分表效果图：</strong><br><img src="https://upload-images.jianshu.io/upload_images/15645795-deb89a831914841d.png" alt="sahrding jdbc_1.png"></p><h3 id="1、spring5-x集成sharding-jdbc依赖"><a href="#1、spring5-x集成sharding-jdbc依赖" class="headerlink" title="1、spring5.x集成sharding jdbc依赖"></a>1、spring5.x集成sharding jdbc依赖</h3><ul><li>sharding jdbc依赖<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- sharding jdbc 分库分表--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>io.shardingsphere<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>sharding-core<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.shardingsphere<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>sharding-jdbc-spring-namespace<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.0-RC2<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure><h3 id="2、sharding-jdbc-xml配置"><a href="#2、sharding-jdbc-xml配置" class="headerlink" title="2、sharding-jdbc xml配置"></a>2、sharding-jdbc xml配置</h3></li></ul><p>jdbc.properties 注意 ，要先创建数据库 ，不用建表 ，jpa自动创建表。</p><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># mysql 数据库公共配置:</span></span><br><span class="line"><span class="meta">mysql.jdbc.driverClassName</span>=<span class="string">com.mysql.cj.jdbc.Driver</span></span><br><span class="line"><span class="comment">#mysql.jdbc.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false</span></span><br><span class="line"><span class="meta">mysql.jdbc.username</span>=<span class="string">root</span></span><br><span class="line"><span class="meta">mysql.jdbc.password</span>=<span class="string">123456</span></span><br><span class="line"><span class="meta">mysql.jdbc.validationQuery</span>=<span class="string">select 'x'</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#自定义算法策略 分库分表</span></span><br><span class="line"><span class="comment">#自定义分表算法-同库：只分表 ，不分库</span></span><br><span class="line"><span class="meta">mysql.jdbc.url9</span>=<span class="string">jdbc:mysql://127.0.0.1:3306/custom_tb?characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#自定义分库算法-只分库 ，不分表</span></span><br><span class="line"><span class="meta">mysql.jdbc.url10</span>=<span class="string">jdbc:mysql://127.0.0.1:3306/custom_ds_0?characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false</span></span><br><span class="line"><span class="meta">mysql.jdbc.url11</span>=<span class="string">jdbc:mysql://127.0.0.1:3306/custom_ds_1?characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false</span></span><br><span class="line"><span class="meta">mysql.jdbc.url12</span>=<span class="string">jdbc:mysql://127.0.0.1:3306/globalDataSource?characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#自定义分库分表算法-分库分表</span></span><br><span class="line"><span class="meta">mysql.jdbc.url13</span>=<span class="string">jdbc:mysql://127.0.0.1:3306/custom_ds_tb_0?characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false</span></span><br><span class="line"><span class="meta">mysql.jdbc.url14</span>=<span class="string">jdbc:mysql://127.0.0.1:3306/custom_ds_tb_1?characterEncoding=UTF-8&amp;serverTimezone=UTC&amp;useSSL=false</span></span><br></pre></td></tr></table></figure><p>druid-common.xml 提取druid数据源公共配置</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">" http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">" http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">" http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">" http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context/spring-context-4.1.xsd"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--指定配置文件的位置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">"classpath:properties/jdbc.properties"</span> <span class="attr">ignore-unresolvable</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- druid数据源公共配置 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"dataSource"</span> <span class="attr">class</span>=<span class="string">"com.alibaba.druid.pool.DruidDataSource"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!--配置mysql--&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--&lt;property name="url" value="$&#123;mysql.jdbc.url&#125;"/&gt;</span></span><br><span class="line"><span class="comment">        &lt;property name="username" value="$&#123;mysql.jdbc.username&#125;"/&gt;</span></span><br><span class="line"><span class="comment">        &lt;property name="password" value="$&#123;mysql.jdbc.password&#125;"/&gt;</span></span><br><span class="line"><span class="comment">        &lt;property name="validationQuery" value="$&#123;mysql.jdbc.validationQuery&#125;"/&gt;</span></span><br><span class="line"><span class="comment">        &lt;property name="driverClassName" value="$&#123;mysql.jdbc.driverClassName&#125;"/&gt;--&gt;</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 配置初始化大小、最小、最大连连接数量 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"initialSize"</span> <span class="attr">value</span>=<span class="string">"10"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"minIdle"</span> <span class="attr">value</span>=<span class="string">"10"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"maxActive"</span> <span class="attr">value</span>=<span class="string">"200"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 配置获取连接等待超时的时间 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"maxWait"</span> <span class="attr">value</span>=<span class="string">"60000"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 配置间隔多久才进行一次检测 ，检测需要关闭的空闲连接 ，单位是毫秒 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"timeBetweenEvictionRunsMillis"</span> <span class="attr">value</span>=<span class="string">"60000"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 配置一个连接在池中最小生存的时间 ，单位是毫秒 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"minEvictableIdleTimeMillis"</span> <span class="attr">value</span>=<span class="string">"600000"</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 配置一个连接在池中最大生存的时间 ，单位是毫秒 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"maxEvictableIdleTimeMillis"</span> <span class="attr">value</span>=<span class="string">"900000"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!--建议配置为 true ，不影响性能 ，并且保证安全性。申请连接的时候检测 ，</span></span><br><span class="line"><span class="comment">        如果空闲时间大于 timeBetweenEvictionRunsMillis ，执行 validationQuery 检测连接是否有效。--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"testWhileIdle"</span> <span class="attr">value</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--申请连接时执行 validationQuery 检测连接是否有效 ，做了这个配置会降低性能。--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"testOnBorrow"</span> <span class="attr">value</span>=<span class="string">"false"</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--归还连接时执行 validationQuery 检测连接是否有效 ，做了这个配置会降低性能。--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"testOnReturn"</span> <span class="attr">value</span>=<span class="string">"false"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!--连接池中的 minIdle 数量以内的连接 ，空闲时间超过 minEvictableIdleTimeMillis ，则会执行 keepAlive 操作。--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"keepAlive"</span> <span class="attr">value</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"phyMaxUseCount"</span> <span class="attr">value</span>=<span class="string">"100000"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 配置监控统计拦截的 filters Druid 连接池的监控信息主要是通过 StatFilter 采集的 ，</span></span><br><span class="line"><span class="comment">        采集的信息非常全面 ，包括 SQL 执行、并发、慢查、执行时间区间分布等--&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--&lt;property name="filters" value="stat,wall"/&gt;--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"filters"</span> <span class="attr">value</span>=<span class="string">"stat"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure><p>custom-sharding-database.xml 分库:只分库 ，不分表配置<br>注意：内部使用的算法 ，请参考项目代码 ，这里不贴出来了 ，下方有github项目地址</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">" http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">" http://www.w3.org/2001/XMLSchema-instance"</span> <span class="attr">xmlns:bean</span>=<span class="string">" http://www.springframework.org/schema/util"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:sharding</span>=<span class="string">" http://shardingsphere.apache.org/schema/shardingsphere/sharding"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">" http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd  http://shardingsphere.apache.org/schema/shardingsphere/sharding  http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--druid公共配置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"classpath:META-INF/spring/druid-common.xml"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"custom_ds_0"</span> <span class="attr">parent</span>=<span class="string">"dataSource"</span> <span class="attr">init-method</span>=<span class="string">"init"</span> <span class="attr">destroy-method</span>=<span class="string">"close"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--配置mysql --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.url10&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.username&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.password&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"validationQuery"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.validationQuery&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driverClassName"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.driverClassName&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"custom_ds_1"</span> <span class="attr">parent</span>=<span class="string">"dataSource"</span> <span class="attr">init-method</span>=<span class="string">"init"</span> <span class="attr">destroy-method</span>=<span class="string">"close"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.url11&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.username&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.password&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"validationQuery"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.validationQuery&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driverClassName"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.driverClassName&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"globalDataSource"</span> <span class="attr">parent</span>=<span class="string">"dataSource"</span> <span class="attr">init-method</span>=<span class="string">"init"</span> <span class="attr">destroy-method</span>=<span class="string">"close"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.url12&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.username&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.password&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"validationQuery"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.validationQuery&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driverClassName"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.driverClassName&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--############标准分片配置-分库：自定义分库策略#############--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 分库策略: 尽量使用sharding:standard-strategy(扩展性强),而不是inline-stragegy--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;sharding:inline-strategy id="databaseStrategy" sharding-column="user_id" algorithm-expression="custom_ds_$&#123;user_id % 2&#125;" /&gt;--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 分库算法：精确分片算法和范围分片算法--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"preciseModuloShardingDatabaseAlgorithm"</span> <span class="attr">class</span>=<span class="string">"com.zja.algorithm.PreciseModuloShardingDatabaseAlgorithm"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"rangeModuloShardingDatabaseAlgorithm"</span> <span class="attr">class</span>=<span class="string">"com.zja.algorithm.RangeModuloShardingDatabaseAlgorithm"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 分表算法：精确分片算法和范围分片算法--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"preciseModuloShardingTableAlgorithm"</span> <span class="attr">class</span>=<span class="string">"com.zja.algorithm.PreciseModuloShardingTableAlgorithm"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"rangeModuloShardingTableAlgorithm"</span> <span class="attr">class</span>=<span class="string">"com.zja.algorithm.RangeModuloShardingTableAlgorithm"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--分库策略：precise-algorithm-ref(必选) ，range-algorithm-ref(可选)--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:standard-strategy</span> <span class="attr">id</span>=<span class="string">"databaseStrategy"</span> <span class="attr">sharding-column</span>=<span class="string">"user_id"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">precise-algorithm-ref</span>=<span class="string">"preciseModuloShardingDatabaseAlgorithm"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">range-algorithm-ref</span>=<span class="string">"rangeModuloShardingDatabaseAlgorithm"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--分表策略：precise-algorithm-ref(必选) ，range-algorithm-ref(可选)--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:standard-strategy</span> <span class="attr">id</span>=<span class="string">"tableStrategy"</span> <span class="attr">sharding-column</span>=<span class="string">"user_id"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">precise-algorithm-ref</span>=<span class="string">"preciseModuloShardingTableAlgorithm"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">range-algorithm-ref</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--工作id--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean:properties</span> <span class="attr">id</span>=<span class="string">"properties"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">prop</span> <span class="attr">key</span>=<span class="string">"worker.id"</span>&gt;</span>123<span class="tag">&lt;/<span class="name">prop</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean:properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--主键生成器：默认使用雪花算法生成递增趋势id--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:key-generator</span> <span class="attr">id</span>=<span class="string">"orderKeyGenerator"</span> <span class="attr">type</span>=<span class="string">"SNOWFLAKE"</span> <span class="attr">column</span>=<span class="string">"order_id"</span> <span class="attr">props-ref</span>=<span class="string">"properties"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:key-generator</span> <span class="attr">id</span>=<span class="string">"itemKeyGenerator"</span> <span class="attr">type</span>=<span class="string">"SNOWFLAKE"</span> <span class="attr">column</span>=<span class="string">"order_item_id"</span> <span class="attr">props-ref</span>=<span class="string">"properties"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--分片数据源--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:data-source</span> <span class="attr">id</span>=<span class="string">"shardingDataSource"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--数据库名称 ，默认数据源globalDataSource ，不分片的数据表放默认数据库中--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">sharding:sharding-rule</span> <span class="attr">data-source-names</span>=<span class="string">"custom_ds_0,custom_ds_1,globalDataSource"</span> <span class="attr">default-data-source-name</span>=<span class="string">"globalDataSource"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">sharding:table-rules</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!-- 分库不分表 --&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:table-rule</span> <span class="attr">logic-table</span>=<span class="string">"t_order"</span> <span class="attr">database-strategy-ref</span>=<span class="string">"databaseStrategy"</span></span></span><br><span class="line"><span class="tag">                                     <span class="attr">key-generator-ref</span>=<span class="string">"orderKeyGenerator"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:table-rule</span> <span class="attr">logic-table</span>=<span class="string">"t_order_item"</span> <span class="attr">database-strategy-ref</span>=<span class="string">"databaseStrategy"</span></span></span><br><span class="line"><span class="tag">                                     <span class="attr">key-generator-ref</span>=<span class="string">"itemKeyGenerator"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">sharding:table-rules</span>&gt;</span></span><br><span class="line"></span><br><span class="line">            <span class="comment">&lt;!-- 绑定表规则列表,表示分库分表的规则相同,这样万一涉及到多个分片的查询,sharding-jdbc就可以确定分库之间不需要不必要的二次关联,所有的查询都应该如此 --&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--绑定表:分片规则一直的主表和子表--&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--t_order表 ，其分片键是order_id ，其子表t_order_item的分片键也是order_id。在规则配置时将两个表配置成绑定关系 ，就不会在查询时出现笛卡尔积--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">sharding:binding-table-rules</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--logic-tables逻辑表名 ，如果真实表为空 ，则把逻辑表名作为真实表名--&gt;</span></span><br><span class="line">                <span class="comment">&lt;!-- 配置绑定表(分片规则相同 ，一般为主表子表的关系) ，若不是绑定表不用配置 ，否则全路由不会走笛卡尔积 --&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:binding-table-rule</span> <span class="attr">logic-tables</span>=<span class="string">"t_order,t_order_item"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">sharding:binding-table-rules</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--广播表:有一些表是没有分片的必要的 ，比如省份信息表 ，全国也就30多条数据 ，这种表在每一个节点上都是一样的 ，这种表叫做广播表。--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">sharding:broadcast-table-rules</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--当插入10条数据 ，会存到每个库中的t_address表中 ，每张表都有完整的表数据10条--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:broadcast-table-rule</span> <span class="attr">table</span>=<span class="string">"t_address"</span>/&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--&lt;sharding:broadcast-table-rule table="t_"/&gt;--&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">sharding:broadcast-table-rules</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">sharding:sharding-rule</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">sharding:data-source</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure><p>custom-sharding-tables.xml 分表：只分表 ，不分库配置<br>注意：内部使用的算法 ，请参考项目代码 ，这里不贴出来了 ，下方有github项目地址</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">" http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">" http://www.w3.org/2001/XMLSchema-instance"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:sharding</span>=<span class="string">" http://shardingsphere.apache.org/schema/shardingsphere/sharding"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:bean</span>=<span class="string">" http://www.springframework.org/schema/util"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">" http://www.springframework.org/schema/beans </span></span></span><br><span class="line"><span class="tag"><span class="string">                         http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">                         http://shardingsphere.apache.org/schema/shardingsphere/sharding</span></span></span><br><span class="line"><span class="tag"><span class="string">                         http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">                         http://www.springframework.org/schema/util</span></span></span><br><span class="line"><span class="tag"><span class="string">                         http://www.springframework.org/schema/util/spring-util.xsd"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--druid公共配置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"classpath:META-INF/spring/druid-common.xml"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"demo_ds"</span> <span class="attr">parent</span>=<span class="string">"dataSource"</span> <span class="attr">init-method</span>=<span class="string">"init"</span> <span class="attr">destroy-method</span>=<span class="string">"close"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--配置mysql --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.url9&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.username&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.password&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"validationQuery"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.validationQuery&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driverClassName"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.driverClassName&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--##########同库分表：只分表 ，不分库############--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 行表达式算法:分表策略 (注：inline-strategy行表达式的策略不利于数据库和表的横向扩展 ，不推荐使用) --&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order_$&#123;order_id % 2&#125;" /&gt;--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:inline-strategy</span> <span class="attr">id</span>=<span class="string">"orderItemTableStrategy"</span> <span class="attr">sharding-column</span>=<span class="string">"order_id"</span> <span class="attr">algorithm-expression</span>=<span class="string">"t_order_item_$&#123;order_id % 2&#125;"</span> /&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 分表策略  精确分片算法 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"myPreciseShardingAlgorithm"</span> <span class="attr">class</span>=<span class="string">"com.zja.myalgorithm.MyPreciseShardingAlgorithm"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 自定义算法：分表策略 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:standard-strategy</span> <span class="attr">id</span>=<span class="string">"orderTableStrategy"</span> <span class="attr">sharding-column</span>=<span class="string">"order_id"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">precise-algorithm-ref</span>=<span class="string">"myPreciseShardingAlgorithm"</span>/&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="tag">&lt;<span class="name">bean:properties</span> <span class="attr">id</span>=<span class="string">"properties"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">prop</span> <span class="attr">key</span>=<span class="string">"worker.id"</span>&gt;</span>123<span class="tag">&lt;/<span class="name">prop</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean:properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--雪花算法生成分布式主键--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:key-generator</span> <span class="attr">id</span>=<span class="string">"orderKeyGenerator"</span> <span class="attr">type</span>=<span class="string">"SNOWFLAKE"</span> <span class="attr">column</span>=<span class="string">"order_id"</span> <span class="attr">props-ref</span>=<span class="string">"properties"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:key-generator</span> <span class="attr">id</span>=<span class="string">"itemKeyGenerator"</span> <span class="attr">type</span>=<span class="string">"SNOWFLAKE"</span> <span class="attr">column</span>=<span class="string">"order_item_id"</span> <span class="attr">props-ref</span>=<span class="string">"properties"</span> /&gt;</span></span><br><span class="line">    </span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:data-source</span> <span class="attr">id</span>=<span class="string">"shardingDataSource"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">sharding:sharding-rule</span> <span class="attr">data-source-names</span>=<span class="string">"demo_ds"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">sharding:table-rules</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--分表策略--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:table-rule</span> <span class="attr">logic-table</span>=<span class="string">"t_order"</span> <span class="attr">actual-data-nodes</span>=<span class="string">"demo_ds.t_order_$&#123;1..2&#125;"</span> <span class="attr">table-strategy-ref</span>=<span class="string">"orderTableStrategy"</span> <span class="attr">key-generator-ref</span>=<span class="string">"orderKeyGenerator"</span>/&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:table-rule</span> <span class="attr">logic-table</span>=<span class="string">"t_order_item"</span> <span class="attr">actual-data-nodes</span>=<span class="string">"demo_ds.t_order_item_$&#123;1..2&#125;"</span> <span class="attr">table-strategy-ref</span>=<span class="string">"orderItemTableStrategy"</span> <span class="attr">key-generator-ref</span>=<span class="string">"itemKeyGenerator"</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">sharding:table-rules</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">sharding:binding-table-rules</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:binding-table-rule</span> <span class="attr">logic-tables</span>=<span class="string">"t_order,t_order_item"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">sharding:binding-table-rules</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">sharding:broadcast-table-rules</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:broadcast-table-rule</span> <span class="attr">table</span>=<span class="string">"t_address"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">sharding:broadcast-table-rules</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">sharding:sharding-rule</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">sharding:data-source</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure><p>custom-sharding-datebase-tables.xml 分库分表配置<br>注意：内部使用的算法 ，请参考项目代码 ，这里不贴出来了 ，下方有github项目地址</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">" http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">" http://www.w3.org/2001/XMLSchema-instance"</span> <span class="attr">xmlns:bean</span>=<span class="string">" http://www.springframework.org/schema/util"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:sharding</span>=<span class="string">" http://shardingsphere.apache.org/schema/shardingsphere/sharding"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">" http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/util  http://www.springframework.org/schema/util/spring-util.xsd  http://shardingsphere.apache.org/schema/shardingsphere/sharding  http://shardingsphere.apache.org/schema/shardingsphere/sharding/sharding.xsd"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--druid公共配置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"classpath:META-INF/spring/druid-common.xml"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"custom_ds_tb_0"</span> <span class="attr">parent</span>=<span class="string">"dataSource"</span> <span class="attr">init-method</span>=<span class="string">"init"</span> <span class="attr">destroy-method</span>=<span class="string">"close"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--配置mysql --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.url13&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.username&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.password&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"validationQuery"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.validationQuery&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driverClassName"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.driverClassName&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"custom_ds_tb_1"</span> <span class="attr">parent</span>=<span class="string">"dataSource"</span> <span class="attr">init-method</span>=<span class="string">"init"</span> <span class="attr">destroy-method</span>=<span class="string">"close"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"url"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.url14&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"username"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.username&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"password"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.password&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"validationQuery"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.validationQuery&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"driverClassName"</span> <span class="attr">value</span>=<span class="string">"$&#123;mysql.jdbc.driverClassName&#125;"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--############标准分片配置：自定义策略#############--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--分库分表策略 ，inline-stragegy(不推荐),推荐使用standard-strategy便于扩展--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;sharding:inline-strategy id="databaseStrategy" sharding-column="user_id" algorithm-expression="demo_ds_$&#123;user_id % 2&#125;" /&gt;</span></span><br><span class="line"><span class="comment">    &lt;sharding:inline-strategy id="orderTableStrategy" sharding-column="order_id" algorithm-expression="t_order_$&#123;order_id % 2&#125;" /&gt;</span></span><br><span class="line"><span class="comment">    &lt;sharding:inline-strategy id="orderItemTableStrategy" sharding-column="order_id" algorithm-expression="t_order_item_$&#123;order_id % 2&#125;" /&gt;--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 分库算法：精确分片算法和范围分片算法--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"preciseModuloShardingDatabaseAlgorithm"</span> <span class="attr">class</span>=<span class="string">"com.zja.algorithm.PreciseModuloShardingDatabaseAlgorithm"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"rangeModuloShardingDatabaseAlgorithm"</span> <span class="attr">class</span>=<span class="string">"com.zja.algorithm.RangeModuloShardingDatabaseAlgorithm"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- 分表算法：精确分片算法和范围分片算法--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"preciseModuloShardingTableAlgorithm"</span> <span class="attr">class</span>=<span class="string">"com.zja.algorithm.PreciseModuloShardingTableAlgorithm"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"rangeModuloShardingTableAlgorithm"</span> <span class="attr">class</span>=<span class="string">"com.zja.algorithm.RangeModuloShardingTableAlgorithm"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--分库策略：precise-algorithm-ref(必选) ，range-algorithm-ref(可选)--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:standard-strategy</span> <span class="attr">id</span>=<span class="string">"databaseStrategy"</span> <span class="attr">sharding-column</span>=<span class="string">"user_id"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">precise-algorithm-ref</span>=<span class="string">"preciseModuloShardingDatabaseAlgorithm"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">range-algorithm-ref</span>=<span class="string">"rangeModuloShardingDatabaseAlgorithm"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--分表策略：precise-algorithm-ref(必选) ，range-algorithm-ref(可选)--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:standard-strategy</span> <span class="attr">id</span>=<span class="string">"orderTableStrategy"</span> <span class="attr">sharding-column</span>=<span class="string">"order_id"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">precise-algorithm-ref</span>=<span class="string">"preciseModuloShardingTableAlgorithm"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">range-algorithm-ref</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:standard-strategy</span> <span class="attr">id</span>=<span class="string">"orderItemTableStrategy"</span> <span class="attr">sharding-column</span>=<span class="string">"order_item_id"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">precise-algorithm-ref</span>=<span class="string">"preciseModuloShardingTableAlgorithm"</span></span></span><br><span class="line"><span class="tag">                                <span class="attr">range-algorithm-ref</span>=<span class="string">""</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean:properties</span> <span class="attr">id</span>=<span class="string">"properties"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">prop</span> <span class="attr">key</span>=<span class="string">"worker.id"</span>&gt;</span>123<span class="tag">&lt;/<span class="name">prop</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean:properties</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--主键生成器：默认使用雪花算法生成递增趋势id--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:key-generator</span> <span class="attr">id</span>=<span class="string">"orderKeyGenerator"</span> <span class="attr">type</span>=<span class="string">"SNOWFLAKE"</span> <span class="attr">column</span>=<span class="string">"order_id"</span> <span class="attr">props-ref</span>=<span class="string">"properties"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:key-generator</span> <span class="attr">id</span>=<span class="string">"itemKeyGenerator"</span> <span class="attr">type</span>=<span class="string">"SNOWFLAKE"</span> <span class="attr">column</span>=<span class="string">"order_item_id"</span> <span class="attr">props-ref</span>=<span class="string">"properties"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--分片数据源--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">sharding:data-source</span> <span class="attr">id</span>=<span class="string">"shardingDataSource"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--分片规则(分库分表) ，默认数据源custom_ds_tb_0 ，不需要分片的数据表放到默认数据源中--&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">sharding:sharding-rule</span> <span class="attr">data-source-names</span>=<span class="string">"custom_ds_tb_0,custom_ds_tb_1"</span> <span class="attr">default-data-source-name</span>=<span class="string">"custom_ds_tb_0"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">sharding:table-rules</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--分库分表--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:table-rule</span> <span class="attr">logic-table</span>=<span class="string">"t_order"</span> <span class="attr">actual-data-nodes</span>=<span class="string">"custom_ds_tb_$&#123;0..1&#125;.t_order_$&#123;0..1&#125;"</span> <span class="attr">database-strategy-ref</span>=<span class="string">"databaseStrategy"</span> <span class="attr">table-strategy-ref</span>=<span class="string">"orderTableStrategy"</span> <span class="attr">key-generator-ref</span>=<span class="string">"orderKeyGenerator"</span> /&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:table-rule</span> <span class="attr">logic-table</span>=<span class="string">"t_order_item"</span> <span class="attr">actual-data-nodes</span>=<span class="string">"custom_ds_tb_$&#123;0..1&#125;.t_order_item_$&#123;0..1&#125;"</span> <span class="attr">database-strategy-ref</span>=<span class="string">"databaseStrategy"</span> <span class="attr">table-strategy-ref</span>=<span class="string">"orderItemTableStrategy"</span> <span class="attr">key-generator-ref</span>=<span class="string">"itemKeyGenerator"</span> /&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">sharding:table-rules</span>&gt;</span></span><br><span class="line">            <span class="comment">&lt;!--logic-tables逻辑表名 ，如果真实表为空 ，则把逻辑表名作为真实表名--&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">sharding:binding-table-rules</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!-- 配置绑定表(分片规则相同 ，一般为主表子表的关系) ，若不是绑定表不用配置 ，否则全路由不会走笛卡尔积 --&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:binding-table-rule</span> <span class="attr">logic-tables</span>=<span class="string">"t_order,t_order_item"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">sharding:binding-table-rules</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">sharding:broadcast-table-rules</span>&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">sharding:broadcast-table-rule</span> <span class="attr">table</span>=<span class="string">"t_address"</span>/&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">sharding:broadcast-table-rules</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">sharding:sharding-rule</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">sharding:data-source</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure><p>spring-data-jpa.xml spring集成jpa并配置”分片数据源”</p><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">beans</span> <span class="attr">xmlns</span>=<span class="string">" http://www.springframework.org/schema/beans"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:xsi</span>=<span class="string">" http://www.w3.org/2001/XMLSchema-instance"</span> <span class="attr">xmlns:tx</span>=<span class="string">" http://www.springframework.org/schema/tx"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:context</span>=<span class="string">" http://www.springframework.org/schema/context"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xmlns:jpa</span>=<span class="string">" http://www.springframework.org/schema/data/jpa"</span></span></span><br><span class="line"><span class="tag">       <span class="attr">xsi:schemaLocation</span>=<span class="string">" http://www.springframework.org/schema/beans</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/beans/spring-beans.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/tx</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/tx/spring-tx.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/context/spring-context.xsd</span></span></span><br><span class="line"><span class="tag"><span class="string">        http://www.springframework.org/schema/data/jpa</span></span></span><br><span class="line"><span class="tag"><span class="string">       https://www.springframework.org/schema/data/jpa/spring-jpa.xsd"</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--############### Sharding-jdbc配置 ###############--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--**************自定义分库分表算法**************--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--分库：自定义分库策略--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--分库：只分库 ，不分表--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;import resource="classpath:META-INF/spring/custom/custom-sharding-database.xml"/&gt;--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--分库分表：分库同时分表--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">import</span> <span class="attr">resource</span>=<span class="string">"classpath:META-INF/spring/custom/custom-sharding-datebase-tables.xml"</span>/&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--同库分表：只分表 ，不分库--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;import resource="classpath:META-INF/spring/custom/custom-sharding-tables.xml"/&gt;--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--指定配置文件的位置--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">context:property-placeholder</span> <span class="attr">location</span>=<span class="string">"classpath:properties/hibernate-jpa.properties"</span> <span class="attr">ignore-unresolvable</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--Start jpa Config #########--&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 配置JPA适配器,实现厂商的特定属性--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"hibernateJpaVendorAdapter"</span> <span class="attr">class</span>=<span class="string">"org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"showSql"</span> <span class="attr">value</span>=<span class="string">"$&#123;adapter.show_sql&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"generateDdl"</span> <span class="attr">value</span>=<span class="string">"$&#123;adapter.generate_ddl&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--关系数据库驱动方言 ，不写自动识别--&gt;</span></span><br><span class="line">        <span class="comment">&lt;!--&lt;property name="databasePlatform" value="$&#123;hibernate.dialect&#125;"/&gt;--&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- 定义实体管理器工厂 Jpa配置 LocalContainerEntityManagerFactoryBean这个选项Spring扮演了容器的角色。完全掌管JPA --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"entityManagerFactory"</span> <span class="attr">class</span>=<span class="string">"org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"</span>&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 指定数据源 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"dataSource"</span> <span class="attr">ref</span>=<span class="string">"shardingDataSource"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">&lt;!-- 指定Jpa持久化实现厂商类,这里以Hibernate为例 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"jpaVendorAdapter"</span> <span class="attr">ref</span>=<span class="string">"hibernateJpaVendorAdapter"</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 指定Entity实体类包路径 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"packagesToScan"</span> <span class="attr">value</span>=<span class="string">"$&#123;hibernate.scan.package&#125;"</span>/&gt;</span></span><br><span class="line">        <span class="comment">&lt;!-- 指定JPA属性；如Hibernate中指定是否显示SQL的是否显示、方言等 --&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"jpaProperties"</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">props</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--关系数据库驱动方言 ，不写自动识别--&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--&lt;prop key="hibernate.dialect"&gt;$&#123;hibernate.dialect&#125;&lt;/prop&gt;--&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--控制台是否打印sql语句--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">prop</span> <span class="attr">key</span>=<span class="string">"hibernate.show_sql"</span>&gt;</span>$&#123;hibernate.show_sql&#125;<span class="tag">&lt;/<span class="name">prop</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--sql语句格式化--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">prop</span> <span class="attr">key</span>=<span class="string">"hibernate.format_sql"</span>&gt;</span>$&#123;hibernate.format_sql&#125;<span class="tag">&lt;/<span class="name">prop</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--启动更新表结构 ，none不用此功能--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">prop</span> <span class="attr">key</span>=<span class="string">"hibernate.hbm2ddl.auto"</span>&gt;</span>$&#123;hibernate.hbm2ddl.auto&#125;<span class="tag">&lt;/<span class="name">prop</span>&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--&lt;prop key="hibernate.hbm2ddl.auto"&gt;create-drop&lt;/prop&gt;--&gt;</span></span><br><span class="line">                <span class="comment">&lt;!--&lt;prop key="hibernate.hbm2ddl.auto"&gt;none&lt;/prop&gt;--&gt;</span></span><br><span class="line">                <span class="comment">&lt;!-- 建表的命名规则： My_NAME-&gt;MyName--&gt;</span></span><br><span class="line">                <span class="tag">&lt;<span class="name">prop</span> <span class="attr">key</span>=<span class="string">"hibernate.ejb.naming_strategy"</span>&gt;</span>$&#123;hibernate.ejb.naming_strategy&#125;<span class="tag">&lt;/<span class="name">prop</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;/<span class="name">props</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">property</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--扫描dao包--&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--&lt;jpa:repositories base-package="com.zja.dao" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="transactionManager" /&gt;--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">jpa:repositories</span> <span class="attr">base-package</span>=<span class="string">"com.zja.dao"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- Jpa 事务配置 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">bean</span> <span class="attr">id</span>=<span class="string">"transactionManager"</span> <span class="attr">class</span>=<span class="string">"org.springframework.orm.jpa.JpaTransactionManager"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">property</span> <span class="attr">name</span>=<span class="string">"entityManagerFactory"</span> <span class="attr">ref</span>=<span class="string">"entityManagerFactory"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">bean</span>&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--XML配置事务声明方式 开启注解声明事务 --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">tx:annotation-driven</span> <span class="attr">transaction-manager</span>=<span class="string">"transactionManager"</span> <span class="attr">proxy-target-class</span>=<span class="string">"true"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!--End jpa Config ########--&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">beans</span>&gt;</span></span><br></pre></td></tr></table></figure><blockquote><p>1.修改数据源<code>dataSource</code>配置,使用不同的分片数据源<br>2.需要修改实体类扫描包和dao接口扫描包路径</p></blockquote><h3 id="3、单元测试-sharding-jdbc-分库分表测试"><a href="#3、单元测试-sharding-jdbc-分库分表测试" class="headerlink" title="3、单元测试(sharding jdbc 分库分表测试)"></a>3、单元测试(sharding jdbc 分库分表测试)</h3><p>分库分表测试：分库分表/只分库/只分表 等三种情况<br>注：实体类代码请参考项目</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.zja.dao.OrderJpaRepositories;</span><br><span class="line"><span class="keyword">import</span> com.zja.entity.Order;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.extension.ExtendWith;</span><br><span class="line"><span class="keyword">import</span> org.junit.platform.runner.JUnitPlatform;</span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.domain.Page;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.domain.PageRequest;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.domain.Pageable;</span><br><span class="line"><span class="keyword">import</span> org.springframework.data.domain.Sort;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.ContextConfiguration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.junit.jupiter.SpringExtension;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.web.WebAppConfiguration;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"><span class="keyword">import</span> java.util.Optional;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Date: 2019-12-17 15:00</span></span><br><span class="line"><span class="comment"> * Author: zhengja</span></span><br><span class="line"><span class="comment"> * Email: zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * Desc：</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(JUnitPlatform<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line"><span class="class">@<span class="title">ExtendWith</span>(<span class="title">SpringExtension</span>.<span class="title">class</span>)</span></span><br><span class="line"><span class="class">@<span class="title">WebAppConfiguration</span></span></span><br><span class="line"><span class="class">@<span class="title">ContextConfiguration</span>(</span>&#123;<span class="string">"classpath*:META-INF/spring/spring-common.xml"</span>&#125;)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DemoOrderTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> OrderJpaRepositories jpaRepositories;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">saveAll</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">        List&lt;Order&gt; orders = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">10</span>;i++)&#123;</span><br><span class="line">            Order order = <span class="keyword">new</span> Order();</span><br><span class="line">            order.setOrderId(i+<span class="number">0L</span>);</span><br><span class="line">            order.setUserId(i);</span><br><span class="line">            order.setStatus(<span class="string">"true"</span>);</span><br><span class="line">            order.setAddressId(i+<span class="number">0L</span>);</span><br><span class="line"></span><br><span class="line">            orders.add(order);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        List&lt;Order&gt; jpas = <span class="keyword">this</span>.jpaRepositories.saveAll(orders);</span><br><span class="line">        System.out.println(jpas);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">save</span><span class="params">()</span></span>&#123;</span><br><span class="line">        Order order = <span class="keyword">new</span> Order();</span><br><span class="line">        <span class="comment">//order_id为偶数 ，插入t_order_1</span></span><br><span class="line">        <span class="comment">//order_id为奇数 ，插入t_order_2</span></span><br><span class="line">        order.setOrderId(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line">        order.setUserId(<span class="number">32</span>);</span><br><span class="line">        order.setStatus(<span class="string">"true"</span>);</span><br><span class="line">        order.setAddressId(<span class="number">32L</span>);</span><br><span class="line">        Order save = <span class="keyword">this</span>.jpaRepositories.save(order);</span><br><span class="line">        System.out.println(save);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Sort</span><span class="params">()</span></span>&#123;</span><br><span class="line">        Sort sort =<span class="keyword">new</span> Sort(Sort.Direction.ASC,<span class="string">"userId"</span>);</span><br><span class="line">        List&lt;Order&gt; orderList = jpaRepositories.findAll(sort);</span><br><span class="line">        System.out.println(orderList);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">Pageable</span><span class="params">()</span></span>&#123;</span><br><span class="line">        Sort sort =<span class="keyword">new</span> Sort(Sort.Direction.ASC,<span class="string">"userId"</span>);</span><br><span class="line">        Pageable pageable = PageRequest.of(<span class="number">0</span>,<span class="number">5</span>,sort);</span><br><span class="line">        Page&lt;Order&gt; orderPage = jpaRepositories.findAll(pageable);</span><br><span class="line">        List&lt;Order&gt; orderList = orderPage.getContent();</span><br><span class="line">        System.out.println(orderList);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><p>广播表测试：每个库中都有此表 ，并且表数据都是完整的 ，适合表数据特别少的情况<br>注：实体类代码请参考项目</p><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> com.zja.dao.AddressJpaRepositories;</span><br><span class="line"><span class="keyword">import</span> com.zja.entity.Address;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.Test;</span><br><span class="line"><span class="keyword">import</span> org.junit.jupiter.api.extension.ExtendWith;</span><br><span class="line"><span class="keyword">import</span> org.junit.platform.runner.JUnitPlatform;</span><br><span class="line"><span class="keyword">import</span> org.junit.runner.RunWith;</span><br><span class="line"><span class="keyword">import</span> org.springframework.beans.factory.annotation.Autowired;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.ContextConfiguration;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.junit.jupiter.SpringExtension;</span><br><span class="line"><span class="keyword">import</span> org.springframework.test.context.web.WebAppConfiguration;</span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> java.util.ArrayList;</span><br><span class="line"><span class="keyword">import</span> java.util.List;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * Date: 2019-12-18 16:35</span></span><br><span class="line"><span class="comment"> * Author: zhengja</span></span><br><span class="line"><span class="comment"> * Email: zhengja@dist.com.cn</span></span><br><span class="line"><span class="comment"> * Desc：</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="meta">@RunWith</span>(JUnitPlatform<span class="class">.<span class="keyword">class</span>)</span></span><br><span class="line"><span class="class">@<span class="title">ExtendWith</span>(<span class="title">SpringExtension</span>.<span class="title">class</span>)</span></span><br><span class="line"><span class="class">@<span class="title">WebAppConfiguration</span></span></span><br><span class="line"><span class="class">@<span class="title">ContextConfiguration</span>(</span>&#123;<span class="string">"classpath*:META-INF/spring/spring-common.xml"</span>&#125;)</span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DemoAddressTest</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Autowired</span></span><br><span class="line">    <span class="keyword">private</span> AddressJpaRepositories jpaRepositories;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">saveAll</span><span class="params">()</span></span>&#123;</span><br><span class="line">        List&lt;Address&gt; addresses = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">        <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">1</span>;i&lt;=<span class="number">10</span>;i++)&#123;</span><br><span class="line">            Address address = <span class="keyword">new</span> Address();</span><br><span class="line">            address.setAddressId(i+<span class="number">0L</span>);</span><br><span class="line">            address.setAddressName(<span class="string">"Name"</span>+i);</span><br><span class="line">            addresses.add(address);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//由于t_address是广播表 ，当插入的数据 ，会存到每个库中的t_address表中 ，每张表都有完整的表数据</span></span><br><span class="line">        List&lt;Address&gt; addressList = jpaRepositories.saveAll(addresses);</span><br><span class="line">        System.out.println(addressList);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">findAll</span><span class="params">()</span></span>&#123;</span><br><span class="line">        List&lt;Address&gt; addressList = <span class="keyword">this</span>.jpaRepositories.findAll();</span><br><span class="line">        System.out.println(addressList);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure><h3 id="4、项目的github和简书博客地址"><a href="#4、项目的github和简书博客地址" class="headerlink" title="4、项目的github和简书博客地址"></a>4、项目的github和简书博客地址</h3><p><strong>github:</strong></p><ul><li><a href="https://links.jianshu.com/go?to=https%3A%2F%2Fgithub.com%2Fzhengjiaao%2Fspring5x" target="_blank" rel="noopener">https://github.com/zhengjiaao/spring5x</a></li></ul><p><strong>博客:</strong></p><ul><li>简书：<a href="https://www.jianshu.com/u/70d69269bd09" target="_blank" rel="noopener">https://www.jianshu.com/u/70d69269bd09</a></li><li>掘金：<a href="https://juejin.im/user/5d82daeef265da03ad14881b/posts" target="_blank" rel="noopener">https://juejin.im/user/5d82daeef265da03ad14881b/posts</a></li></ul></div><div class="popular-posts-header"><p style="font-size:18px;color:red;margin:0 0 5px 0">相关文章推荐：</p></div><ul class="popular-posts"><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\2680579140.html" rel="bookmark">Spring5x-base</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\974023075.html" rel="bookmark">Spring5x-swagger2</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\1894473216.html" rel="bookmark">spring5-x-memcached</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\3173602028.html" rel="bookmark">spring5-x-mongodb</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\2137420170.html" rel="bookmark">spring5-x-rabbimq-高级篇</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\2872074402.html" rel="bookmark">spring5-x-redis</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\1537235376.html" rel="bookmark">spring5-x-rabbitmq-基础</a></div></li><li class="popular-posts-item"><div class="popular-posts-title"><a href="\posts\3838648753.html" rel="bookmark">spring5-x-springmvc</a></div></li></ul><div><div style="text-align:center;color:#ccc;font-size:14px">-------------本文结束<i class="fa fa-paw"></i>感谢您的阅读-------------</div></div><div class="reward-container"><div><p style="font-size:14px;color:#34495e;margin:0 0 5px 0">赞赏一下吧～ 还可以关注公众号订阅最新内容</p></div><button disable="enable" onclick="var qr = document.getElementById(&quot;qr&quot;); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">打赏</button><div id="qr" style="display:none"><div style="display:inline-block"><img src="/my-images/WeChat.png" alt="宇宙小神特别萌 微信支付"><p>微信支付</p></div><div style="display:inline-block"><img src="/my-images/Alipay.png" alt="宇宙小神特别萌 支付宝"><p>支付宝</p></div></div></div><footer class="post-footer"><div class="post-tags"><a href="/tags/spring5-xml%E6%96%B9%E5%BC%8F%E9%85%8D%E7%BD%AE/" rel="tag"><i class="fa fa-tag"></i> spring5-xml方式配置</a></div><div class="post-nav"><div class="post-nav-item"><a href="/posts/3792233199.html" rel="prev" title="Tomcat自动重启脚本-vbs"><i class="fa fa-chevron-left"></i> Tomcat自动重启脚本-vbs</a></div><div class="post-nav-item"><a href="/posts/3526231086.html" rel="next" title="spring5x-mybatis-senior">spring5x-mybatis-senior <i class="fa fa-chevron-right"></i></a></div></div></footer></article></div></div><div class="comments" id="valine-comments"></div><script>window.addEventListener('tabs:register', () => {
    let activeClass = CONFIG.comments.activeClass;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }</script></div><div class="toggle sidebar-toggle"><span class="toggle-line toggle-line-first"></span> <span class="toggle-line toggle-line-middle"></span> <span class="toggle-line toggle-line-last"></span></div><aside class="sidebar"><div class="sidebar-inner"><ul class="sidebar-nav motion-element"><li class="sidebar-nav-toc">文章目录</li><li class="sidebar-nav-overview">站点概览</li></ul><div class="post-toc-wrap sidebar-panel"><div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#搭建项目"><span class="nav-text">搭建项目</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#1、spring5-x集成sharding-jdbc依赖"><span class="nav-text">1、spring5.x集成sharding jdbc依赖</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2、sharding-jdbc-xml配置"><span class="nav-text">2、sharding-jdbc xml配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3、单元测试-sharding-jdbc-分库分表测试"><span class="nav-text">3、单元测试(sharding jdbc 分库分表测试)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4、项目的github和简书博客地址"><span class="nav-text">4、项目的github和简书博客地址</span></a></li></ol></li></ol></div></div><div class="site-overview-wrap sidebar-panel"><div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person"><img class="site-author-image" itemprop="image" alt="宇宙小神特别萌" src="/my-images/avatar.gif"><p class="site-author-name" itemprop="name">宇宙小神特别萌</p><div class="site-description" itemprop="description">人生就是一次幸运的旅行！</div></div><div class="site-state-wrap motion-element"><nav class="site-state"><div class="site-state-item site-state-posts"><a href="/archives/"><span class="site-state-item-count">116</span> <span class="site-state-item-name">文章</span></a></div><div class="site-state-item site-state-tags"><a href="/tags/"><span class="site-state-item-count">27</span> <span class="site-state-item-name">标签</span></a></div></nav></div><div class="links-of-author motion-element"><span class="links-of-author-item"><a href="https://github.com/zhengjiaao" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;zhengjiaao" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a> </span><span class="links-of-author-item"><a href="https://www.jianshu.com/u/70d69269bd09" title="简书 → https:&#x2F;&#x2F;www.jianshu.com&#x2F;u&#x2F;70d69269bd09" rel="noopener" target="_blank"><i class="fa fa-fw fa-book"></i>简书</a> </span><span class="links-of-author-item"><a href="/atom.xml" title="RSS → &#x2F;atom.xml"><i class="fa fa-fw fa-rss"></i>RSS</a></span></div><div class="links-of-blogroll motion-element"><div class="links-of-blogroll-title"><i class="fa fa-fw fa-link"></i> 推荐阅读</div><ul class="links-of-blogroll-list"><li class="links-of-blogroll-item"><a href="https://github.com/spring-projects" title="https:&#x2F;&#x2F;github.com&#x2F;spring-projects" rel="noopener" target="_blank">spring-projects</a></li><li class="links-of-blogroll-item"><a href="http://www.alloyteam.com/nav/" title="http:&#x2F;&#x2F;www.alloyteam.com&#x2F;nav&#x2F;" rel="noopener" target="_blank">Web前端导航</a></li><li class="links-of-blogroll-item"><a href="http://www.chuangzaoshi.com/code" title="http:&#x2F;&#x2F;www.chuangzaoshi.com&#x2F;code" rel="noopener" target="_blank">创造狮导航</a></li></ul></div></div></div></aside><div id="sidebar-dimmer"></div></div></main><footer class="footer"><div class="footer-inner"><div class="copyright">&copy; <span itemprop="copyrightYear">2020</span> <span class="with-love"><i class="fa fa-heart"></i> </span><span class="author" itemprop="copyrightHolder">宇宙小神特别萌</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-area-chart"></i> </span><span title="站点总字数">1.2m</span> <span class="post-meta-divider">|</span> <span class="post-meta-item-icon"><i class="fa fa-coffee"></i> </span><span title="站点阅读时长">17:41</span></div><div class="busuanzi-count"><script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span class="post-meta-item" id="busuanzi_container_site_uv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-user"></i> </span><span class="site-uv" title="总访客量"><span id="busuanzi_value_site_uv"></span> </span></span><span class="post-meta-divider">|</span> <span class="post-meta-item" id="busuanzi_container_site_pv" style="display:none"><span class="post-meta-item-icon"><i class="fa fa-eye"></i> </span><span class="site-pv" title="总访问量"><span id="busuanzi_value_site_pv"></span></span></span></div></div></footer></div><script src="//cdn.jsdelivr.net/npm/animejs@3.1.0/lib/anime.min.js"></script><script src="//cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js"></script><script src="//cdn.jsdelivr.net/gh/fancyapps/fancybox@3/dist/jquery.fancybox.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/velocity/1.2.1/velocity.ui.min.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script>!function(){var t=document.createElement("script"),e=window.location.protocol.split(":")[0];t.src="https"===e?"https://zz.bdstatic.com/linksubmit/push.js":"http://push.zhanzhang.baidu.com/push.js";var s=document.getElementsByTagName("script")[0];s.parentNode.insertBefore(t,s)}()</script><script src="/js/local-search.js"></script><script>NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//cdnjs.cloudflare.com/ajax/libs/valine/1.3.10/Valine.min.js', () => {
    <!--var GUEST = ['nick', 'mail', 'link'];-->
    var GUEST = ['nick', 'mail'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el: '#valine-comments',
      verify: false,
      notify: false,
      appId: 'OuyHeA0XGL8hK4vgbqL0iERx-gzGzoHsz',
      appKey: 'L58VglKmWULcGK2W2AN7kWzM',
      placeholder: "留下邮箱，有回复时你将收到提醒，邮箱不会被公开。",
      avatar: 'wavatar',
      meta: guest,
      pageSize: '10' || 10,
      visitor: true,
      lang: 'zh-cn' || 'zh-cn',
      path: location.pathname,
      recordIP: true,
      serverURLs: ''
    });
  }, window.Valine);
});</script><script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({pluginRootPath:"live2dw/",pluginJsPath:"lib/",pluginModelPath:"assets/",tagMode:!1,model:{jsonPath:"/live2dw/assets/wanko.model.json"},display:{position:"right",width:150,height:300},mobile:{show:!1},log:!1})</script></body><script type="text/javascript" src="/js/src/activetitle.js"></script></html><!-- rebuild by neat -->